from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from PIL import Image
import time

class test():
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://www.geetest.com/demo/slide-float.html')
        self.driver.maximize_window()
        self.wait = WebDriverWait(self.driver, 60, 0.3)
        self.grap_img = r'zimg/grap_img.png'
        self.img = r'zimg/img.png'

    def parse(self):
        self.wait.until(
            EC.presence_of_element_located((By.XPATH, '//div[@id="captcha"]/div[3]/div[3]')),
            message='未找到'
        )
        self.driver.find_element(By.XPATH, value='//div[@id="captcha"]/div[3]/div[3]').click()
        time.sleep(1)
        self.driver.execute_script('document.getElementsByClassName("geetest_div_slice geetest_absolute")[0].style="background-image: url(&quot;https://static.geetest.com/pictures/gt/7bfaaa72b/slice/56a075dec.png&quot;); left: 0px; top: 23px; width: 59px; height: 59px; transform: translate(0px, 0px); display: none;"')

        div_img = self.driver.find_element(By.CLASS_NAME, value='geetest_window')
        time.sleep(1)
        div_img.screenshot(self.grap_img)

        self.driver.execute_script('document.getElementsByClassName("geetest_div_fullbg geetest_fade geetest_absolute")[0].style="height: 160px; display: block; opacity: 1;"')
        div_img = self.driver.find_element(By.CLASS_NAME, value='geetest_window')
        time.sleep(1)
        div_img.screenshot(self.img)
        time.sleep(1)
        self.driver.execute_script('document.getElementsByClassName("geetest_div_slice geetest_absolute")[0].style="background-image: url(&quot;https://static.geetest.com/pictures/gt/7bfaaa72b/slice/56a075dec.png&quot;); left: 0px; top: 23px; width: 59px; height: 59px; transform: translate(0px, 0px); display: block;"')
        self.driver.execute_script('document.getElementsByClassName("geetest_div_fullbg geetest_fade geetest_absolute")[0].style="height: 160px; display: none; opacity: 1;"')


    def save_img(self):
        grap_img = Image.open(self.grap_img)
        img = Image.open(self.img)
        print(img.size)
        x,y = img.size
        for i in range(x):
            for j in range(y):
                if img.getpixel((i, j))[0]-grap_img.getpixel((i, j))[0] > 60 and img.getpixel((i, j))[1]-grap_img.getpixel((i, j))[1] > 60 and img.getpixel((i, j))[2]-grap_img.getpixel((i, j))[2] > 60:
                    return i, j

    def move(self, i):
        button = self.driver.find_element(By.CLASS_NAME, value='geetest_slider_button')
        wb = ActionChains(self.driver).click_and_hold(button)
        wb.move_by_offset(i, 0).release()
        wb.release(button).perform()

    def main(self):
        self.parse()
        i,j = self.save_img()
        self.move(i)
        print(i,j)



if __name__ == '__main__':
    test().main()